package com.yc.nfc;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016-07-11.
 */
public class Convert {
    //-------------------------------------------------------
    // 判断奇数或偶数，位运算，最后一位是1则为奇数，为0是偶数
    static public int isOdd(int num)
    {
        return num & 0x1;
    }
    //-------------------------------------------------------
    static public int HexToInt(String inHex)//Hex字符串转int
    {
        return Integer.parseInt(inHex, 16);
    }
    //-------------------------------------------------------
    static public byte HexToByte(String inHex)//Hex字符串转byte
    {
        return (byte) Integer.parseInt(inHex,16);
    }
    //-------------------------------------------------------
    static public String Byte2Hex(Byte inByte)//1字节转2个Hex字符
    {
        return String.format("%02x", inByte).toUpperCase();
    }
    //-------------------------------------------------------
    static public String ByteArrToHex(byte[] inBytArr , String spilt)//字节数组转转hex字符串
    {
        StringBuilder strBuilder=new StringBuilder();
        int j=inBytArr.length;
        for (int i = 0; i < j; i++)
        {
            strBuilder.append(Byte2Hex(inBytArr[i]));
            if(spilt!=null)strBuilder.append(spilt);
        }
        return strBuilder.toString();
    }
    static public String ByteArrToHex(byte[] inBytArr){
        return ByteArrToHex(inBytArr," ");
    }
    //-------------------------------------------------------
    static public String ByteArrToHex(byte[] inBytArr, int offset, int byteCount)//字节数组转转hex字符串，可选长度
    {
        StringBuilder strBuilder=new StringBuilder();
        int j=byteCount;
        for (int i = offset; i < j; i++)
        {
            strBuilder.append(Byte2Hex(inBytArr[i]));
        }
        return strBuilder.toString();
    }
    //-------------------------------------------------------
    //转hex字符串转字节数组
    static public byte[] HexToByteArr(String inHex)//hex字符串转字节数组
    {
        int hexlen = inHex.length();
        byte[] result;
        if (isOdd(hexlen)==1)
        {//奇数
            hexlen++;
            result = new byte[(hexlen/2)];
            inHex="0"+inHex;
        }else {//偶数
            result = new byte[(hexlen/2)];
        }
        int j=0;
        for (int i = 0; i < hexlen; i+=2)
        {
            result[j]=HexToByte(inHex.substring(i,i+2));
            j++;
        }
        return result;
    }

    public static String BCD2Str(short bcd){
        return Integer.toHexString(bcd);
    }

    public static byte Dec2BCD(int bcd){
        String bcdStr = String.valueOf(bcd);
        return Byte.parseByte(bcdStr, 16);
    }

    public static byte[] Date2BCD(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String strDate =format.format(date);
        byte[] ret = new byte[strDate.length()];
        for (int i = 0;i<strDate.length()/2;i++){
            String sub = strDate.substring(i*2,i*2+2);
            ret[i] = Byte.parseByte(sub, 16);
        }
        return ret;
    }
}
